//
//  FeedbackViewController.swift
//  ddx0513
//
//  Created by ejian  on 15/6/4.
//  Copyright (c) 2015年 jiang yongbin. All rights reserved.
//

import UIKit

class FeedbackViewController: UIViewController, UITextViewDelegate {
    var textPlacehold = true
    var hud: MBProgressHUD?

    @IBOutlet weak var countLabel: UILabel!
    @IBOutlet weak var textView: UITextView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        textView.text = "欢迎提出您的意见或遇到的问题，我们会及时帮您解决"
        textView.textColor = UIColor.darkGrayColor()
        
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    @IBAction func viewTapGuesture(sender: AnyObject) {
        self.textView.resignFirstResponder()
    }
    
    func textViewDidBeginEditing(textView: UITextView) {
        if (textPlacehold) {
            textPlacehold = false
            textView.text = ""
            textView.textColor = UIColor.blackColor()
        }
    }
    
    func textViewDidEndEditing(textView: UITextView) {
        if (!textPlacehold && count(textView.text) == 0) {
            textPlacehold = true
            textView.text = "欢迎提出您的意见或遇到的问题，我们会及时帮您解决"
            textView.textColor = UIColor.darkGrayColor()
        }
    }
    
    func textViewDidChange(textView: UITextView) {
        if (textPlacehold) {
            countLabel.text = "还可以输入240个字"
        } else {
            var str = textView.text
            var num = count(str)
            if (num > 240) {
                let range = Range(start: str.startIndex, end: advance(str.startIndex, 240))
                self.textView.text = str.substringWithRange(range)
                countLabel.text = "还可以输入0个字"
            } else {
                countLabel.text = "还可以输入\(240 - num)个字"
            }
        }
    }
    

    @IBAction func submitButtonPressed(sender: AnyObject) {
        if (textPlacehold || self.textView.text == "") {
            ViewUtils.popMessage(self, title: "", message: "反馈意见不能为空")
            
            return
        }
        
        startIndicator()
        let a = ["userid": DBUtils.mem.userId, "token": DBUtils.mem.token, "context": self.textView.text]
        NetUtils.netRequest(Method.POST, URLString: NetUtils.getURLFeedback(), parameters: (a as! [String : AnyObject]), view: self.view, responseHandler: stopIndicator, successHandler: save)
    }
    
    @IBAction func backButtonPressed(sender: AnyObject) {
        self.navigationController?.popViewControllerAnimated(true)

    }
    
    //MARK: - 网络回调函数
    
    func startIndicator() {
        hud = MBProgressHUD(view: self.view)
        self.view.addSubview(hud!)
        hud?.labelText = "提交中.."
        hud?.show(true)
    }
    
    func stopIndicator() {
//        hud?.hide(false)
    }
    
    func save(json: JSON) {
        hud!.mode = MBProgressHUDMode.Text
        hud!.labelText = "提交成功"
        hud!.margin = 10.0
        hud!.removeFromSuperViewOnHide = true
        hud!.square = true
        
        hud?.showWhileExecuting(Selector("save2"), onTarget: self, withObject: nil, animated: true)
    }
    
    func save2() {
        sleep(1)
        dispatch_sync(dispatch_get_main_queue(), { () -> Void in
            self.navigationController?.popViewControllerAnimated(true)
        })
    }
    
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
